﻿@model WebSettingModel;
<div id="websetting" class="page" v-cloak>
    <Tabs ref="tabs">
        <TabPane label="SEO信息">
            <i-form ref="form" v-model="submitting" action="@Model.SaveUrl" class="am-form-h box box-form" @@success="submitSuccess">
                <form-item label="网站名称" label-for="SiteName" :required="true">
                    <input type="text" name="SiteName" id="SiteName" required />
                </form-item>
                <form-item label="首页标题" label-for="Title">
                    <input type="text" name="Title" id="Title" placeholder="不填则按：网站名称+关键词显示" />
                </form-item>
                <form-item label="内页标题显示方式" label-for="PageTitleShowType" :required="true">
                    <select name="PageTitleShowType" id="PageTitleShowType" required>
                        @foreach (var item in Model.PageTitleShowTypes)
                        {
                            <option value="@item.id">@item.text</option>
                        }
                    </select>
                </form-item>
                <form-item label="网站关键字" label-for="Keyword">
                    <input type="text" name="Keyword" id="Keyword" placeholder="每个关键词用小写“,”号隔开" />
                </form-item>
                <form-item label="网站描述" label-for="Description">
                    <input type="text" name="Description" id="Description" />
                </form-item>
                <form-item label="页脚信息" label-for="Footer">
                    <editor ref="footer" name="Footer" style="width:100%;height:100px;" type="small" @@on-ready="editorReady()"></editor>
                </form-item>
                <div class="am-form-group" style="text-align:center;padding:30px 0">
                    <i-button type="submit" style="padding:8px 30px" :loading="submitting">保存设置</i-button>
                </div>
            </i-form>
        </TabPane>
        <TabPane label="网站设置">
            <i-form ref="form1" v-model="submitting" action="@Model.SaveUrl" class="am-form-h box box-form" @@success="submitSuccess">
                <form-item label="文件大小限制(MB)" label-for="MaxFileSize">
                    <input type="number" name="MaxFileSize" id="MaxFileSize" placeholder="单位：MB" />
                </form-item>
                <form-item label="文件类型白名单" label-for="WhiteFileExtensions">
                    <input-tag name="WhiteFileExtensions"></input-tag>
                </form-item>
                <form-item label="网站统计" label-for="EnableSiteStatistic">
                    <i-switch name="EnableSiteStatistic" v-model="enableStatistic">
                        <span slot="open">开启</span>
                        <span slot="close">关闭</span>
                    </i-switch>
                </form-item>
                <form-item label="统计数据保留时间" label-for="StatisticSaveTime" v-if="enableStatistic">
                    <input type="number" name="StatisticSaveTime" id="StatisticSaveTime" placeholder="单位：天" />
                </form-item>
                <form-item label="第三方统计代码" label-for="StatisticScript">
                    <textarea style="width:100%;height:50px" name="StatisticScript"  data-type="script"></textarea>
                </form-item>
                <form-item label="网站错误日志" label-for="EnableErrorLog">
                    <i-switch name="EnableErrorLog" v-model="enableErrorLog">
                        <span slot="open">开启</span>
                        <span slot="close">关闭</span>
                    </i-switch>
                </form-item>
                <form-item label="日志保留时间" v-if="enableErrorLog">
                    <input type="number" name="ErrorLogSaveTime" placeholder="单位：天" />
                </form-item>
                <form-item label="登陆日志保留时间">
                    <input type="number" name="LoginLogSaveTime" placeholder="单位：天" />
                </form-item>
                <form-item label="网站脚本" label-for="Scripts">
                    <textarea style="width:100%;height:50px" name="Scripts" data-type="script"></textarea>
                </form-item>
                <form-item label="关闭网站" label-for="CloseSite">
                    <i-switch name="CloseSite" v-model="closeSite">
                        <span slot="open">是</span>
                        <span slot="close">否</span>
                    </i-switch>
                </form-item>
                <form-item label="关闭提示内容" label-for="CloseContent" v-show="closeSite">
                    <editor name="CloseContent" style="width:100%;height:80px" type="small" @@on-ready="editorReady1()"></editor>
                </form-item>
                <form-item label="关闭提示内容（英文）" label-for="CloseContentEn" v-show="closeSite">
                    <editor name="CloseContentEn" style="width:100%;height:80px" type="small" @@on-ready="editorReady1()"></editor>
                </form-item>
                <form-item label="IP黑名单" label-for="BlockList">
                    <textarea name="BlockList" style="width:100%;height:80px" placeholder="一行一个IP！" data-delimiter="\n"></textarea>
                </form-item>
                <div class="am-form-group" style="text-align:center;padding:30px 0">
                    <i-button type="submit" style="padding:8px 30px" :loading="submitting">保存设置</i-button>
                </div>
            </i-form>
        </TabPane>
        <TabPane label="缓存设置">
            <i-form class="am-form-h box box-form">
                <form-item label="开启缓存">
                    <i-switch v-model="cacheSetting.EnableCache">
                        <span slot="open">开启</span>
                        <span slot="close">关闭</span>
                    </i-switch>
                </form-item>
                <form-item label="开启Html压缩">
                    <i-switch v-model="cacheSetting.EnableMinification" :disabled="!cacheSetting.EnableCache">
                        <span slot="open">开启</span>
                        <span slot="close">关闭</span>
                    </i-switch>
                </form-item>
                <form-item label="开启GZip压缩">
                    <i-switch v-model="cacheSetting.EnableCompression" :disabled="!cacheSetting.EnableCache">
                        <span slot="open">开启</span>
                        <span slot="close">关闭</span>
                    </i-switch>
                </form-item>
                <form-item label="缓存时间（秒）" label-for="CacheTime">
                    <input type="number" name="CacheTime" v-model="cacheSetting.CacheTime" placeholder="单位：秒" />
                </form-item>
                <form-item label="页面大小限制（KB）" label-for="MaxResponseSize">
                    <input type="number" name="MaxResponseSize" v-model="cacheSetting.MaxResponseSize" placeholder="超时设置大小则不缓存（单位KB）" />
                </form-item>
                <form-item label="不缓存页面" label-for="IgnorePages">
                    <textarea name="IgnorePages" style="height:80px" placeholder="每行一个页面地址" :value="ignorePages" @@change="handleIgnorePages($event)"></textarea>
                </form-item>
                <form-item label="缓存页面设置">
                    <table style="width:100%">
                        <tr>
                            <td>页面地址：</td>
                            <td style="width:150px;">缓存时间（秒）：</td>
                            <td style="width:300px">Url参数：</td>
                            <td style="width:100px">&nbsp;</td>
                        </tr>
                        <tr>
                            <td><input type="text" v-model="cachePage.Url" /></td>
                            <td style="padding-left: 18px;"><input type="text" v-model="cachePage.CacheTime" /></td>
                            <td style="padding-left: 18px;"><input-tag v-model="cachePage.VaryByParams"></input-tag></td>
                            <td style="padding-left: 18px;"><i-button skin="info" @@click="handleAddOrUpdateCachePage" style="width: 100%;padding: .58em 1em;">{{editCachePage?'更新':'添加'}}</i-button></td>
                        </tr>
                    </table>
                    <table class="am-table am-table-hover" v-show="cacheSetting.CachePages&&cacheSetting.CachePages.length">
                        <thead>
                            <tr>
                                <th>&nbsp;</th>
                                <th>页面地址</th>
                                <th>缓存时间</th>
                                <th>Url参数</th>
                                <th>编辑</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr v-for="(item, index) in cacheSetting.CachePages" :key="item">
                                <td>{{index+1}}.</td>
                                <td>{{item.Url}}</td>
                                <td>{{item.CacheTime}}秒</td>
                                <td>{{item.VaryByParams&&item.VaryByParams.length?item.VaryByParams.join(','):''}}</td>
                                <td style="text-align:right">
                                    <a @@click="handleEditCachePage(item)" class="am-icon-edit" style="color:#00b1ff;margin-right:10px" title="编辑"></a>
                                    <a @@click="handleDelCachePage(item)" class="am-icon-remove" style="color:red;" title="删除"></a>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </form-item>
                <div class="am-form-group" style="text-align:center;padding:30px 0">
                    <i-button type="button" style="padding:8px 30px" :loading="submitting" @@click="handleCacheSetting">保存设置</i-button>
                </div>
            </i-form>
        </TabPane>
        <TabPane label="邮件服务">
            <i-form ref="email_setting_form" v-model="submitting" action="@Model.EmailSaveUrl" class="am-form-h box box-form" @@success="submitSuccess">
                <form-item label="SMTP地址" label-for="SMTP" :required="true">
                    <div class="am-u-sm-8">
                        <input type="text" name="SMTP" id="SMTP" required />
                    </div>
                    <div class="am-u-sm-2 am-padding-left-0">
                        <input type="number" name="Port" placeholder="默认端口：25" />
                    </div>
                    <div class="am-u-sm-2 am-padding-left-0">
                        <i-switch name="EnableSSL">
                            <span slot="open">使用SSL</span>
                            <span slot="close">禁用SSL</span>
                        </i-switch>
                    </div>
                </form-item>
                <form-item label="用户名" label-for="Username" :required="true">
                    <input type="text" name="Username" id="Username" required />
                </form-item>
                <form-item label="密码" label-for="Password">
                    <input type="password" name="Password" id="Password" required />
                </form-item>
                <form-item label="发件人名称" label-for="SenderName">
                    <div class="am-u-md-6">
                        <input type="text" name="SenderName" id="SenderName" placeholder="中文发件人名称" required />
                    </div>
                    <div class="am-u-md-6">
                        <input type="text" name="SenderNameEn"placeholder="英文发件人名称" />
                    </div>
                </form-item>
                <form-item label="发件人Email" label-for="SenderEmail">
                    <input type="text" name="SenderEmail" id="SenderEmail" />
                </form-item>
                <form-item label="模板替换地址" label-for="EmailUrl">
                    <input type="text" name="Url" id="EmailUrl" />
                </form-item>
                <form-item label="Email测试">
                    <div class="am-u-md-8">
                        <input type="text" id="TestEmail" placeholder="请输入接收邮件的Email地址..." />
                    </div>
                    <div class="am-u-md-4">
                        <i-button type="button" style="padding:8px 30px" :loading="submitting" @@click="handleSendMail">发送测试邮件</i-button>
                    </div>
                </form-item>
                <div class="am-form-group" style="text-align:center;padding:30px 0;">
                    <i-button type="submit" style="padding:8px 30px" :loading="submitting">保存设置</i-button>
                </div>
            </i-form>
        </TabPane>
        @if (Model.IsAdmin)
        {
            <TabPane label="系统参数">
                <i-form ref="admin_form" v-model="submitting" action="@Model.SaveUrl" class="am-form-h box box-form" @@success="submitSuccess">
                    <form-item label="清除缓存" label-for="CacheName">
                        <div class="am-input-group am-input-group-primary">
                            <input type="text" name="CacheName" id="CacheName" class="am-form-field" value="" />
                            <span class="am-input-group-btn">
                                <button type="button" class="am-btn am-btn-primary" @@click="handleClearCache">清除</button>
                            </span>
                        </div>
                    </form-item>
                    <form-item label="后台标题" label-for="AdminTitle" :required="true">
                        <input type="text" name="AdminTitle" id="AdminTitle" />
                    </form-item>
                    <form-item label="后台短标题" label-for="AdminSubTitle" :required="true">
                        <input type="text" name="AdminSubTitle" id="AdminSubTitle" />
                    </form-item>
                    <form-item label="网站备份文件夹" label-for="AdminBackupFolders">
                        <input-tag name="AdminBackupFolders"></input-tag>
                    </form-item>
                    <form-item label="网站备份路径" label-for="AdminBackupPath">
                        <input type="text" name="AdminBackupPath" id="AdminBackupPath" />
                    </form-item>
                    <form-item label="最多备份数量" label-for="BackupLimit">
                        <input type="number" name="BackupLimit" id="BackupLimit" />
                    </form-item>
                    <form-item label="数据缓存时间(秒)" label-for="DataCacheTime">
                        <input type="number" name="DataCacheTime" id="DataCacheTime" placeholder="单位：秒" />
                    </form-item>
                    <div class="am-form-group" style="text-align:center;padding:30px 0;">
                        <i-button type="submit" style="padding:8px 30px" :loading="submitting">保存设置</i-button>
                    </div>
                </i-form>
            </TabPane>
            <TabPane label="CMS注册">
                <i-form class="am-form-h box box-form">
                    <form-item label="AppKey">
                        <input type="text" v-model="websetting.AppKey" />
                    </form-item>
                    <form-item label="AppSecret">
                        <input type="text" v-model="websetting.AppSecret" readonly />
                    </form-item>
                    <form-item label="绑定域名" :required="true">
                        <textarea style="height:80px" placeholder="每行一个域名，最多设置三个" :value="domains" @@change="handleDomains($event)"></textarea>
                    </form-item>
                    <form-item label="注册密码" :required="true">
                        <input ref="reg_cms_token" type="password" />
                    </form-item>
                    <div class="am-form-group" style="text-align:center;padding:30px 0">
                        <i-button type="button" style="padding:8px 30px" :loading="submitting" @@click="handleRegCMS">{{websetting.AppSecret?'更新域名':'注册CMS'}}</i-button>
                    </div>
                </i-form>
            </TabPane>
        }
    </Tabs>
</div>
@section script {
    @Html.GetEditorScripts()
    <script type="text/javascript">
        new Vue({
            el: '#websetting',
            data: {
                submitting: false,
                enableStatistic: @Model.WebSetting.EnableSiteStatistic.ToString().ToLower(),
                enableErrorLog: @Model.WebSetting.EnableErrorLog.ToString().ToLower(),
                closeSite: @Model.WebSetting.CloseSite.ToString().ToLower(),
                websetting: @Html.ToJson(Model.WebSetting),
                emailsetting: @Html.ToJson(Model.EmailSetting),
                cacheSetting: @Html.ToJson(Model.CacheSetting),
                cachePage: { Url: '', CacheTime: 30, VaryByParams: [] },
                editCachePage: null,
            },
            computed: {
                ignorePages: function () {
                    if (this.cacheSetting.IgnorePages && this.cacheSetting.IgnorePages.length)
                        return this.cacheSetting.IgnorePages.join('\n');
                    return '';
                },
                domains: function () {
                    if (this.websetting.BindDomains && this.websetting.BindDomains.length)
                        return this.websetting.BindDomains.join('\n');
                    return '';
                }
            },
            methods: {
                handleSendMail: function () {
                    var self = this;
                    var email = $('#TestEmail').val();
                    if (!email)
                    {
                        this.$Message.error('请输入Email地址!');
                        return;
                    }
                    this.submitting = true;
                    $.post('@Model.SendMailUrl', { email: email }, function (result) {
                        if (result.succ)
                        {
                            self.$Message.success('Email发送成功！');
                        } else {
                            self.$Message.error('发送失败：' + result.data);
                        }
                    }).always(function () {
                        self.submitting = false;
                    });
                },
                handleRegCMS: function () {
                    var self = this;
                    this.submitting = true;
                    $.post('@Model.RegCMSUrl', { token: this.$refs.reg_cms_token.value, domains: this.websetting.BindDomains }, function (result) {
                        if (result.succ) {
                            self.$Message.success("注册成功");
                            self.$set(self.websetting, "AppSecret", result.data);
                        }
                        else
                            self.$Message.error(result.data);
                    }).always(function () {
                        self.submitting = false;
                    });
                },
                handleDomains: function (evt) {
                    if (evt.target.value)
                        this.websetting.BindDomains = evt.target.value.split('\n');
                    else
                        this.websetting.BindDomains = [];
                },
                handleIgnorePages: function (evt) {
                    var value = evt.target.value;
                    this.cacheSetting.IgnorePages = value.split('\n');
                },
                handleClearCache: function () {
                    $.post('@Model.ClearCacheUrl', { key: $('#CacheName').val() }, function (result) {
                        this.$Message.success(result);
                    });
                },
                handleCacheSetting: function () {
                    var self = this;
                    this.submitting = true;
                    $.post('@Model.CacheSaveUrl', { cacheSetting: this.cacheSetting }, function (result) {
                        if (result.succ)
                            self.$Message.success("更新成功");
                        else
                            self.$Message.error(result.data);
                    }).always(function () {
                        self.submitting = false;
                    });
                },
                handleAddOrUpdateCachePage: function () {
                    var newObj = JSON.parse(JSON.stringify(this.cachePage));
                    if (this.editCachePage) {
                        this.editCachePage.Url = newObj.Url;
                        this.editCachePage.CacheTime = newObj.CacheTime;
                        this.editCachePage.VaryByParams = newObj.VaryByParams;
                        this.editCachePage = null;
                    } else {
                        if (!this.cacheSetting.CachePages)
                            this.cacheSetting.CachePages = [];
                        this.cacheSetting.CachePages.push(newObj);
                    }
                    this.cachePage.Url = '';
                    this.cachePage.VaryByParams = [];
                },
                handleEditCachePage: function (item) {
                    this.editCachePage = item;
                    this.cachePage.Url = this.editCachePage.Url;
                    this.cachePage.CacheTime = this.editCachePage.CacheTime;
                    this.cachePage.VaryByParams = this.editCachePage.VaryByParams;
                },
                handleDelCachePage: function (item) {
                    this.cacheSetting.CachePages.remove(item);
                },
                submitSuccess: function (data) {
                    this.$Message.success("更新成功");
                },
                editorReady: function () {
                    this.$refs.form.fill(this.websetting);
                    this.$refs.form1.fill(this.websetting);
                },
                editorReady1: function () {
                    if (!this.$refs.form1.readyCount)
                        this.$refs.form1.readyCount = 0;
                    this.$refs.form1.readyCount++;
                    if (this.$refs.form1.readyCount == 2) {
                        this.$refs.form1.fill(this.websetting);
                    }
                }
            },
            mounted: function () {
                this.$refs.email_setting_form.fill(this.emailsetting);
                if (this.$refs.admin_form) {
                    this.$refs.admin_form.fill(this.websetting);
                }
            }
        });
    </script>
}